package com.servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.UserInfo;
import com.dao.HdfsDao;
//import com.dao.HdfsHADao;
import com.dao.UserDao;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private UserDao userDao=new UserDao();
	private HdfsDao hdfsDao=new HdfsDao();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flag=request.getParameter("flag");
		
		if("login".equals(flag)){
			this.login(request,response);
		}
		
		else if("register".equals(flag)){
			this.register(request,response);
		}
		
		else if("logout".equals(flag)){
			this.logout(request,response);
		}
	}

	//退出登录
	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.getSession().invalidate();
		response.getWriter().write("<script>window.top.location.href='login.jsp'</script>");
	}

	//用户登录
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
			
		UserInfo user=userDao.getLoginUser(userName,password);
		if(user!=null){
			request.getSession().setAttribute("g_user", user);
			request.getRequestDispatcher("/index.html").forward(request, response);
		}
		else{
			request.setAttribute("msg", "账号或密码错误,登录失败!");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}
	
	//用户注册
	private void register(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String userName=request.getParameter("userName");
		
		UserInfo oldUser=userDao.getUserByName(userName);
		if(oldUser!=null){
			request.setAttribute("msg", "对不起,该用户名已被其他用户注册!");
			request.setAttribute("regesterFlag", false);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return ;
		}

		String password=request.getParameter("password");
		String phone=request.getParameter("phone");
		
		UserInfo user=new UserInfo();
		user.setUserName(userName);
		user.setPassword(password);
		user.setPhone(phone);
		
		if(userDao.addUser(user)==1){
			//用户登录后,还要在hdfs创建这个用户的文件夹,以用户名做为文件夹名称
			boolean result=hdfsDao.createUserRoot(userName);
			
			if(result==true){
				request.setAttribute("regesterFlag", true);
				request.setAttribute("msg", "用户注册成功!");
			}
			else{
				userDao.delUser(userName);
				request.setAttribute("regesterFlag", false);
				request.setAttribute("msg", "用户注册失败!");
			}
			
			request.getRequestDispatcher("/register.jsp").forward(request, response);
		}
	}
	
	

}
